/*******************************************************************************
 * Copyright (c) 2012 Daniele Parisi.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Public License v3.0
 * which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/gpl.html
 * 
 * Contributors:
 *     Daniele Parisi
 *     neb.inf@gmail.com
 *     http://www.danieleparisi.com/
 ******************************************************************************/
package org.jxreport.core.views.chart.bar;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

import org.jxreport.core.Row;
import org.jxreport.core.Table;
import org.jxreport.core.XMLUtils;
import org.jxreport.core.XMLValues;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

public class Series {

	String title;
	List<String> values = new LinkedList<String>();
	
	public Series(Element seriesElement) {
		this.setTitle(XMLUtils.getTextValue(seriesElement, XMLValues.TITLE));
		
		NodeList nl = seriesElement.getElementsByTagName(XMLValues.VALUE);
		for(int i = 0 ; i < nl.getLength();i++) {
			Element el = (Element)nl.item(i);
			this.values.add(el.getTextContent());
		}
	}
	
	public Series(Element seriesElement, Table table) {

		NodeList nl = seriesElement.getElementsByTagName(XMLValues.VALUE_INDEX);
		for(int i = 0 ; i < nl.getLength();i++) {
			Element el = (Element)nl.item(i);
			int valueIndex = Integer.valueOf(el.getTextContent());
			
			List<String> values = new ArrayList<String>();
			for (Row row : table) {
				Object value = row.get(valueIndex).getValue();
				if(value instanceof String)
					values.add((String)value);
				else if(value instanceof Double)
					values.add(String.valueOf((Double)value));
			}
		}
		this.values.addAll(values);
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public List<String> getValues() {
		return values;
	}

	public void setValues(List<String> values) {
		this.values = values;
	}
}
